//
//  ForumViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/25.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "ForumViewController.h"
#import "ForumTableViewCell.h"
#import "loginVC.h"
#import "AllCommentVC.h"
#import "ForumDetailVC.h"
#import "HTTPManagers.h"
#import "ForumModel.h"
#import "MJExtension.h"
#import "BBSViewController.h"



@interface ForumViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation ForumViewController
{
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getForumData];
    
}
-(void)getForumData{
//    http://23.234.49.240/api/topic/3?alive=off
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"alive"]=@"off";
    
    
    [HTTPManagers getWithForum:@"3" params:params success:^(id json) {
        [_tableView.mj_header endRefreshing];
        NSArray *message = json[@"message"];
        if (self.dataArr.count>0) {
            [self.dataArr removeAllObjects];
        }
        for (int i =0; i<message.count; i++) {
            NSDictionary *dict = message[i];
            ForumModel *model = [ForumModel mj_objectWithKeyValues:dict];
            [self.dataArr addObject:model];
        }
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

}
-(void)setupUI{
    [self setRightBtn];
    [self.view addSubview:self.tableView];
}
-(void)setRightBtn{
    
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    login.size = CGSizeMake(40, 15);
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:RGBCOLOR(0x25, 0x43, 0x7f) forState:UIControlStateNormal];
    [login addTarget:self
                 action:@selector(showLoginView)
       forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:login];
}
-(void)showLoginView{
    loginVC *login = [[loginVC alloc]init];
    [self.navigationController pushViewController:login animated:YES];

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    ForumDetailVC *detail = [[ForumDetailVC alloc]init];
//    detail.url = @"http://www.baidu.com";
    ForumModel *model = self.dataArr[indexPath.row];
    BBSViewController *aBBSViewController = [[BBSViewController alloc]init];
    aBBSViewController.mtitle = model.Title;
    aBBSViewController.mUrlStr = [NSString stringWithFormat:@"%ld",model.Id];
    [self.navigationController pushViewController:aBBSViewController animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ForumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ForumTableViewCell class]) forIndexPath:indexPath];
    if (self.dataArr.count>0) {
        
        ForumModel *model = self.dataArr[indexPath.row];
        NSString *titleStr = [model.Title stringByReplacingOccurrencesOfString:@" " withString:@""];
        titleStr = [titleStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        [cell customTitle:titleStr likeCount:model.Like commentCount:model.CommentCount];
        cell.tid = model.Id;
        cell.model = model;
    }
    
    return cell;
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ForumTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ForumTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getForumData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
            
            [_tableView.mj_footer endRefreshing];
        }];
    }
    return _tableView;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
